from django.contrib import admin
from .models import *
from apps.base.models import Supplier, SupplierContacts


@admin.register(SupplierQuotation)
class SupplierQuotationAdmin(admin.ModelAdmin):
    list_display = ('A001','F002', 'B001', 'supplier', 'B002', 'B003', 'B004', 'B005')
    search_fields = ('F002__D001', 'A001','F002__F002__B001')

    def save_model(self, request, obj, form, change):
        if not obj.A001:
            obj.addId()
        super().save_model(request, obj, form, change)


# 供应商图片、联系人和供应商同页显示
class SupplierPictureInline(admin.TabularInline):
    model = SupplierPicture


class SupplierContactsInline(admin.TabularInline):
    model = SupplierContacts


@admin.register(Supplier)
class SupplierAdmin(admin.ModelAdmin):
    list_display = ('A001','B002', 'B003', 'B004', 'B006')

    list_per_page = 30

    ordering = ('A001',)
    inlines = [SupplierPictureInline, SupplierContactsInline, ]
    search_fields = ('B001',)


# 采购单从表和采购单主表放在一起
class PurchaseDetailInline(admin.TabularInline):
    readonly_fields = ('surplus',)
    raw_id_fields = ('F002',)
    model = PurchaseDetail


@admin.register(PurchaseMaster)
class PurchaseMasterAdmin(admin.ModelAdmin):
    list_display = ('A001', 'A003', 'F002')
    inlines = [PurchaseDetailInline, ]
    search_fields = ('A001', 'F002',)
    autocomplete_fields = ('F002',)


@admin.register(SampleStock)
class SampleStockAdmin(admin.ModelAdmin):
    list_display = ('F002','F001', 'specs', 'amount', 'B005')
    search_fields = ('F002__D001',)


@admin.register(Receipt)
class ReceiptAdmin(admin.ModelAdmin):
    list_display = ('A001','supplier','F002', 'B001', 'B002', 'B003','F001')
    search_fields = ('F002__D001','A001')
